﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;

public partial class Main_Reports_Accounts_OutStandings_JournalLedgers : System.Web.UI.Page
{

    Datautility du = new Datautility();
    AccountSettings ac = new AccountSettings();
    double dblOriginal=0;
    double dblPendingAmt = 0;
    double dblReceivedPaid = 0;
    string strvalue = string.Empty;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            binddata();
            hidFinyear.Value = Session["FinYear"].ToString();
            txtToDate.Text = "31/03/" + (Convert.ToInt32(Session["FinYear"].ToString()) + 1).ToString();
            
               
                lblCompname.Text = Session["compname"].ToString();
            
            
        }
       
       
    }
    protected void binddata()
    {
        DataTable dt = du.getdata("Select LID,LNAME from ACC_Ledmast");
        grdDisplay.DataSource = dt;
        grdDisplay.DataBind();
       
    }
    protected void grd_Display(string strvalue, string ToDate)
    {
        for (int i = 1; i < grdDisplay.Rows.Count; i++)
        {

            CheckBox checkbox = (CheckBox)grdDisplay.Rows[i].FindControl("CheckBox1");

            HiddenField hid = (HiddenField)grdDisplay.Rows[i].FindControl("hidLID");
             if (checkbox.Checked == true)
            {
                strvalue += hid.Value + ",";

            }
        }
        
        if (strvalue != string.Empty && ToDate != string.Empty)
        {
            lblHeading.Text = "Outstanding View for Selected Ledgers as on " + string.Format("{0:dd-MMM-yyyy}", Convert.ToDateTime(ToDate));
            SqlParameter [] pr={new SqlParameter("@LEDGERID",strvalue),
                                   new SqlParameter("@TODATE",string.Format("{0:MM/dd/yyyy}",Convert.ToDateTime(ToDate)))
                               };
           
            DataTable dt = du.getdatausingprocedure("USP_ACC_GetOutstandingGeneralLedgers", pr);
          
            grdDisplay1.DataSource = dt;
            grdDisplay1.DataBind();
            panel1.Visible = true;
            panel.Visible = false;
            panelch.Visible = true;
        }
    }

    protected void btnOk_Click(object sender, EventArgs e)
    {
    
        
        grd_Display(strvalue, txtToDate.Text);


    }
    protected void rowdatabound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            if (e.Row.Cells[5].Text != string.Empty)
            {
                dblOriginal += Math.Abs(Convert.ToDouble(e.Row.Cells[5].Text));
                if (Math.Abs(Convert.ToDouble(e.Row.Cells[5].Text)) != 0)
                {
                    e.Row.Cells[5].Text = ac.GetCurrency(Math.Abs(Convert.ToDouble(e.Row.Cells[5].Text)));

                }
                else
                {
                    e.Row.Cells[5].Text = string.Empty;
                }
            }
            if (e.Row.Cells[7].Text != string.Empty)
            {
                dblReceivedPaid += Math.Abs(Convert.ToDouble(e.Row.Cells[7].Text));

                if (Math.Abs(Convert.ToDouble(e.Row.Cells[7].Text)) != 0)
                {
                    e.Row.Cells[7].Text = ac.GetCurrency(Math.Abs(Convert.ToDouble(e.Row.Cells[7].Text)));
                }
                else
                    e.Row.Cells[7].Text = string.Empty;
            }
            if (e.Row.Cells[8].Text != string.Empty)
            {
                dblPendingAmt += Math.Abs(Convert.ToDouble(e.Row.Cells[8].Text));

                if (Math.Abs(Convert.ToDouble(e.Row.Cells[8].Text)) != 0)
                {
                    e.Row.Cells[8].Text = ac.GetCurrency(Math.Abs(Convert.ToDouble(e.Row.Cells[8].Text)));
                }
                else
                    e.Row.Cells[8].Text = string.Empty;
            }


        }
        if (e.Row.RowType == DataControlRowType.Footer)
        {
            e.Row.Cells[4].Text = "Total";
            e.Row.Cells[5].Text = ac.GetCurrency(dblOriginal).ToString();
            e.Row.Cells[7].Text = ac.GetCurrency(dblReceivedPaid).ToString();
            e.Row.Cells[8].Text = ac.GetCurrency(dblPendingAmt).ToString();
            e.Row.HorizontalAlign = HorizontalAlign.Right;
            e.Row.Font.Bold = true;
        }
    }




    protected void btnShow_Click(object sender, EventArgs e)
    {   

        grd_Display(strvalue, txtToDate.Text); 
    }
    protected void lnkback_Click(object sender, EventArgs e)
    {
        Response.Redirect("outstandings.aspx");
    }
}
